using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Acms.Bll;

public partial class Users_Default : BaseUserPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.MembershipAdministrator, Role.Roles.MembershipViewer }; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        gridUsers.ItemDataBound += new Telerik.Web.UI.GridItemEventHandler(gridUsers_ItemDataBound);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var showCreateLink = true;
            bool.TryParse(ConfigurationManager.AppSettings["ShowCreateUsersLink"], out showCreateLink);
            lnkCreate.Visible = showCreateLink;
        }
    }

    void gridUsers_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is Telerik.Web.UI.GridDataItem)
        {
            var gridDataItem = (Telerik.Web.UI.GridDataItem)e.Item;
            var user = gridDataItem.DataItem as User;
            if (user != null)
            {
                var deleteColumItem = gridDataItem["DeleteColumn"];
                if (deleteColumItem != null)
                {
                    if (User != null && User.Identity != null && User.Identity.User != null)
                    {
                        //hide delete user button if its a system wide user ie SiteID = 0
                        if (user.Id == User.Identity.User.Id || user.SiteId == 0)
                        {
                            deleteColumItem.Controls[0].Visible = false;
                        }
                        else
                        {
                            deleteColumItem.Controls[0].Visible = true;
                        }
                    }
                    else
                    {
                        deleteColumItem.Controls[0].Visible = false;
                    }
                }
                else
                {
                    throw new NullReferenceException("Unable to find \"DeleteColumn\" in column collection");
                }
                
            }
        }
    }

    protected void dsUsers_SelectData(object sender, Acms.DataControls.SelectEventArgs<User> e)
    {
        List<User> users = Acms.Bll.User.Search(AcmsContext.SiteId);
        Acms.Bll.User.Sort(users, e.SortExpression);
        e.SetData(users);
    }
}
